package tech.yunqian.slim.project.service;

import java.util.List;
import java.util.concurrent.Future;

import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class AsyncHttpRequestService {
    @Async
    public Future<String> post(String url, List<String> headers, JSONObject payload) {
        String response = null;
        HttpRequest httpRequest = new HttpRequest(url, "POST");
        httpRequest.contentType("application/json", "UTF-8");
        httpRequest.send(payload.toJSONString());
        Integer code = httpRequest.code();
        if (code == 200) {
            response = httpRequest.body();
            log.debug(response);
        }
        return new AsyncResult<>(response);
    }
}
